<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Login extends MX_Controller
{
    private $_langCode;
    
    public function __construct()
    {
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->library('Admin_Layout');
        $this->load->library('Minifier');
        $this->load->language('common', $this->_langCode);
    }
	
    public function index()
    {
        if($this->session->userdata('u_id')) {
            redirect('admin/category');
        }
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->set_error_delimiters('<ul class="errors"><li>','</li></ul>');
        $this->form_validation->set_rules('Username', 'tên đăng nhập', 'required|min_length[5]');
        $this->form_validation->set_rules('Password', 'mật khẩu', 'required|min_length[6]');
        $this->form_validation->set_message('required', ucfirst('%s') . ' không được để trống');
        $this->form_validation->set_message('min_length', ucfirst('%s') . ' phải nhập ít nhất %d ký tự');
        if ($this->form_validation->run() == TRUE) {
            if($this->input->post()) {
                $user = $this->input->post('Username');
                $pass = $this->input->post('Password');
                $back = $this->input->get('back');
                $pass = md5($pass);
                $this->load->model('user/user_model');
                $check = $this->user_model->checkLogin($user, $pass);
                if($check['num_row'] == 1) {
                    $this->session->set_userdata('u_id', $check['data'][0]['u_id']);
                    $this->session->set_userdata('u_acc', $check['data'][0]['u_acc']);
                    $this->session->set_userdata('u_email', $check['data'][0]['u_email']);
                    if($back) {
                        $backLink = urldecode($back);
                        redirect($backLink);
                    }
                    redirect(site_url('admin/category'));
                } else {
                    $data['error'] = 'Thông tin chưa chính xác !';
                }
            }
        }
        $this->admin_layout->setCssJs();
        $this->minifier->AddJs('jquery.validate');
        $data['headInfo'] = $this->lang->line('head_infomation');
        $data['common_lang']  = $this->lang->line('admin_lang_common');
        $data['combineCss'] = $this->minifier->GetCombined('css');
        $data['combineJs']  = $this->minifier->GetCombined('js');
        $this->load->view('login/login_view', $data);
    }
    
    public function logout()
    {
        $this->session->unset_userdata('u_id');
        $this->session->unset_userdata('u_acc');
        $this->session->unset_userdata('u_email');
        redirect(site_url('admin'));
    }

}

?>
